package no.nordicsemi.android.nrfmesh.database.dao;

import androidx.lifecycle.LiveData; // 修改点：新增 LiveData 观察支持
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.List;

import no.nordicsemi.android.nrfmesh.database.entity.LocalAccount;

@Dao
public interface LocalAccountDao {

    @Query("SELECT * FROM local_accounts ORDER BY lastLoginAt DESC")
    List<LocalAccount> list();

    // 修改点：提供可观察的账户列表，登录页顶部下拉可用它自动刷新
    @Query("SELECT * FROM local_accounts ORDER BY lastLoginAt DESC")
    LiveData<List<LocalAccount>> observeAll();

    @Query("SELECT * FROM local_accounts WHERE userId=:userId LIMIT 1")
    LocalAccount get(String userId);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void upsert(LocalAccount a);

    @Query("DELETE FROM local_accounts WHERE userId=:userId")
    void delete(String userId);

    @Query("UPDATE local_accounts SET lastLoginAt=:ts WHERE userId=:userId")
    void touch(String userId, long ts);
}
